"""
路由包
包含所有API路由定义
"""

import importlib
import inspect
import os

from fastapi import APIRouter

routers = []


def _load_routers():
    """
    加载当前目录下所有的_router.py结尾的文件中的fastapi router,并放到routers中
    """
    current_dir = os.path.dirname(__file__)

    for filename in os.listdir(current_dir):
        if filename.endswith("_router.py") and filename != "__init__.py":
            module_name = filename[:-3]  # 去掉.py后缀
            try:
                # 动态导入模块
                module = importlib.import_module(f".{module_name}", package=__package__)

                # 查找模块中的router对象
                for name, obj in inspect.getmembers(module):
                    if isinstance(obj, APIRouter) and hasattr(obj, "routes"):
                        routers.append(obj)
                        break
            except ImportError as e:
                print(f"Failed to import {module_name}: {e}")


_load_routers()
